package com.gz.university.crm.web.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.BeanUtils;

import com.gz.university.crm.annotation.Limit;
import com.gz.university.crm.bean.DataType;
import com.gz.university.crm.entity.SysMenu;
import com.gz.university.crm.entity.SysMenuPrivilege;
import com.gz.university.crm.entity.SysPopedom;
import com.gz.university.crm.entity.SysPopedomPrivilege;
import com.gz.university.crm.entity.SysRole;
import com.gz.university.crm.form.SysRoleForm;
import com.gz.university.crm.service.ISysMenuPrivilegeService;
import com.gz.university.crm.service.ISysMenuService;
import com.gz.university.crm.service.ISysPopedomPrivilegeService;
import com.gz.university.crm.service.ISysPopedomService;
import com.gz.university.crm.service.ISysRoleService;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("serial")
@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "sysRoleAction")
@Results({
		@Result(name = "list", location = "/WEB-INF/page/sys/role/list.jsp"),
		@Result(name = "add", location = "/WEB-INF/page/sys/role/add.jsp"),
		@Result(name = "toListAction", type = "redirect", location = "sysRoleAction!list.action"),
		@Result(name = "edit", location = "/WEB-INF/page/sys/role/edit.jsp"),
		@Result(name = "listPopedom", location = "/WEB-INF/page/sys/role/popedom/view.jsp"),
		@Result(name = "updatePopedom", params = { "actionName",
				"sysRoleAction", "namespace", "/", "method", "listPopedom" }, type = "chain"),
		@Result(name = "listMenu", location = "/WEB-INF/page/sys/role/menu/view.jsp"),
		@Result(name = "updateMenu", params = { "actionName", "sysRoleAction",
				"namespace", "/", "method", "listMenu" }, type = "chain") })
public class SysRoleAction extends BaseAction implements
		ModelDriven<SysRoleForm> {
	private static final Logger LOGGER = Logger.getLogger(SysRoleAction.class);
	private SysRoleForm sysRoleForm = new SysRoleForm();
	private ISysRoleService sysRoleService;
	private ISysPopedomService sysPopedomService;
	private ISysPopedomPrivilegeService sysPopedomPrivilegeService;
	private ISysMenuService sysMenuService;
	private ISysMenuPrivilegeService sysMenuPrivilegeService;

	public ISysMenuPrivilegeService getSysMenuPrivilegeService() {
		return sysMenuPrivilegeService;
	}

	@Resource(name = ISysMenuPrivilegeService.SERVICE_NAME)
	public void setSysMenuPrivilegeService(
			ISysMenuPrivilegeService sysMenuPrivilegeService) {
		this.sysMenuPrivilegeService = sysMenuPrivilegeService;
	}

	public ISysMenuService getSysMenuService() {
		return sysMenuService;
	}

	@Resource(name = ISysMenuService.SERVICE_NAME)
	public void setSysMenuService(ISysMenuService sysMenuService) {
		this.sysMenuService = sysMenuService;
	}

	public ISysPopedomPrivilegeService getSysPopedomPrivilegeService() {
		return sysPopedomPrivilegeService;
	}

	@Resource(name = ISysPopedomPrivilegeService.SERVICE_NAME)
	public void setSysPopedomPrivilegeService(
			ISysPopedomPrivilegeService sysPopedomPrivilegeService) {
		this.sysPopedomPrivilegeService = sysPopedomPrivilegeService;
	}

	public ISysPopedomService getSysPopedomService() {
		return sysPopedomService;
	}

	@Resource(name = ISysPopedomService.SERVICE_NAME)
	public void setSysPopedomService(ISysPopedomService sysPopedomService) {
		this.sysPopedomService = sysPopedomService;
	}

	public ISysRoleService getSysRoleService() {
		return sysRoleService;
	}

	@Resource(name = ISysRoleService.SERVICE_NAME)
	public void setSysRoleService(ISysRoleService sysRoleService) {
		this.sysRoleService = sysRoleService;
	}

	@Override
	public SysRoleForm getModel() {
		// TODO Auto-generated method stub
		return sysRoleForm;
	}

	/**
	 * 显示权限组的添加页面
	 * 
	 * @return
	 */
	@Limit(module = "role", privilege = "add")
	public String add() {
		return "add";
	}

	/**
	 * 保存权限
	 * 
	 * @return
	 */
	@Limit(module = "role", privilege = "save")
	public String save() {
		// 实例化POJO对象
		SysRole sysRole = new SysRole();
		// 复制VO－－－－>POJO
		BeanUtils.copyProperties(sysRoleForm, sysRole,
				new String[] { "sysRoleId" });
		// 调用权限组的service层方法
		this.sysRoleService.saveSysrole(sysRole);
		return "toListAction";
	}

	/**
	 * 查询操作权限组的信息
	 * 
	 * @return
	 */
	@Limit(module = "role", privilege = "list")
	public String list() {
		// 调用业务层的方法查询,获取系统的权限资源
		List<SysRole> sysRoles = this.sysRoleService.findSysRoles(sysRoleForm);
		this.getRequest().setAttribute("sysRoles", sysRoles);
		return "list";
	}

	/**
	 * 跳转到部门修改页面，并为部门修改页面准备数据
	 * 
	 * @return
	 */
	@Limit(module = "role", privilege = "edit")
	public String edit() {
		if (sysRoleForm.getSysRoleId() != null) {
			SysRole sysRole = this.sysRoleService.findSysRoleById(sysRoleForm
					.getSysRoleId());
			BeanUtils.copyProperties(sysRole, sysRoleForm);
		}
		return "edit";
	}

	/**
	 * 权限资源组的数据，调转到list()展示更改后的列表显示 更改
	 * 
	 * @return
	 */
	@Limit(module = "role", privilege = "update")
	public String update() {
		// 实例化POJO
		SysRole sysRole = new SysRole();
		// 将VO转化为POJO
		BeanUtils.copyProperties(sysRoleForm, sysRole);
		this.sysRoleService.updateSysRole(sysRole);
		return "toListAction";
	}

	/**
	 * 
	 * @return
	 */
	@Limit(module = "role", privilege = "delete")
	public String delete() {
		// 获取需要删除的ids
		String[] ids = this.getRequest().getParameterValues("ids");
		Integer[] deleteIds = DataType.convertStringArrayToIntegerArray(ids);
		this.sysRoleService.deleteSysRolesByIds(deleteIds);

		return "toListAction";
	}

	/* 处理系统权限的操作功能--开始 */
	/**
	 * 获取系统所有的操作功能
	 * 
	 * @return
	 */
	@Limit(module = "role", privilege = "listPopedom")
	public String listPopedom() {
		// 获取角色roleId
		String roleIdString = this.getRequest().getParameter("roleId");
		if (StringUtils.isNotBlank(roleIdString)) {
			Integer roleId = Integer.parseInt(roleIdString);
			SysRole sysRole = this.sysRoleService.findSysRoleById(roleId);
			this.getRequest().setAttribute("sysRole", sysRole);
			/* 获取系统的所有功能（数据来源于操作功能表） */
			List<SysPopedom> sysPopedoms = this.sysPopedomService
					.findAllSysPopedoms();
			this.getRequest().setAttribute("sysPopedoms", sysPopedoms);
			// 查询该权限组包含的权限
			List<SysPopedomPrivilege> sysPopedomPrivileges = this.sysPopedomPrivilegeService
					.findSysPopedomPrivilegesByRoleId(roleId);
			this.getRequest().setAttribute("sysPopedomPrivileges",
					sysPopedomPrivileges);
			return "listPopedom";
		}
		return null;
	}

	/**
	 * 
	 * @return
	 */
	@Limit(module = "role", privilege = "updatePopedom")
	public String updatePopedom() {
		String roleIdString = this.getRequest().getParameter("roleId");
		Integer roleId = Integer.parseInt(roleIdString);
		SysRole sysRole = this.sysRoleService.findSysRoleById(roleId);
		this.getRequest().setAttribute("sysRole", sysRole);
		// 获取复选框的值
		String[] popedomModules = this.getRequest().getParameterValues(
				"popedomModule");
		System.out.println("updatepopedom----------------");
		// this.sysPopedomPrivilegeService.deletePopedomPrivilegesByRoleId(roleId,popedomModules);
		this.sysPopedomPrivilegeService.updatePopedom(roleId, popedomModules);
		System.out.println("updatepopedom----------------");
		return "updatePopedom";
	}

	/* 处理系统权限的操作功能--結束 */
	/* 处理系统权限的菜单显示 */
	/**
	 * 显示获取权限的菜单
	 * 
	 * @return
	 */
	public String listMenu() {
		// 获取权限ID，roleId
		String roleIdString = this.getRequest().getParameter("roleId");
		if (StringUtils.isNotBlank(roleIdString)) {
			Integer roleId = Integer.parseInt(roleIdString);
			SysRole sysRole = this.sysRoleService.findSysRoleById(roleId);
			// 将系统的权限ID返回到页面
			this.getRequest().setAttribute("sysRole", sysRole);
			List<SysMenu> sysMenus = this.sysMenuService.findAllSysMenus();
			// 将系统中所有的菜单返回显示在页面上面
			this.getRequest().setAttribute("sysMenus", sysMenus);
			// 通过权限ID查询SysMenuPrivilege
			List<SysMenuPrivilege> sysMenuPrivileges = this.sysMenuPrivilegeService
					.findSysMenuPrivilegesByRoleId(roleId);
			this.getRequest().setAttribute("sysMenuPrivileges", sysMenuPrivileges);
			return "listMenu";
		}
		return null;
	}

	/**
	 * 更新系统的菜单
	 * 
	 * @return
	 */
	public String updateMenu() {
		// 获取权限ID，roleId
		String roleIdString = this.getRequest().getParameter("roleId");
		if (StringUtils.isNotBlank(roleIdString)) {
			Integer roleId = Integer.parseInt(roleIdString);
			SysRole sysRole = this.sysRoleService.findSysRoleById(roleId);
			// 将系统的权限ID返回到页面
			this.getRequest().setAttribute("sysRole", sysRole);
			// 获取选中的复选框
			String[] menuModules = this.getRequest().getParameterValues(
					"menuModule");
			// 调用设置菜单权限的业务层
			this.sysMenuPrivilegeService.updateMenu(roleId, menuModules);
			return "updateMenu";
		}
		return null;
	}
}
